<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :imageList="goods.mainPictures"></XtxImageView>
          <!-- 统计数量 -->
          <GoodsSale :goods="goods"></GoodsSale>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品信息区 -->
          <GoodsInfo :goods="goods"></GoodsInfo>
          <!-- sku组件 -->
          <!-- <GoodsSku
            :goods="goods"
            @change="handleChange"
            skuId="1369155862131642369"
          ></GoodsSku> -->
          <!-- skuId="1369155862131642369" 为啥去掉？？？ -->
          <GoodsSku :goods="goods" @change="handleChange"></GoodsSku>
          <!-- vue2:  :value="count"
              子组件里接收: props (父=> 子)
              this.$emit('input', 更新的值)   (子=> 父)
              v-model自动帮咱们注册了 @input事件, 会自动更新值,赋值给绑定的变量
          -->
          <!-- vue3中  :modelValue="count" modelValue名字是固定的
                子组件通过props去接收 ( 父=> 子)
                子组件里  context.emit('update:modelValue', 更新的值)
                v-model 自动注册了 @update:modelValue事件, 自动更新值,赋值给绑定变量
                少写了@自定义事件
           -->
          <XtxNumbox v-model="count" :max="countMax"></XtxNumbox>
          <!-- <XtxNumbox :modelValue="count" @update:modelValue="自动给count赋值了" :max="countMax" ></XtxNumbox> -->
          <XtxButton
            @click="insertCart"
            type="primary"
            style="margin-top: 20px"
          >
            加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品详情 -->
          <GoodsDetail :goods="goods" />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsSku from './components/goods-sku.vue'

import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from 'vuex'

import { message } from '@/components/Message'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsSale,
    GoodsInfo,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    // vue2 this.$route.query/params
    // route语义化的可以随便定义(但是不要随便定义)
    const route = useRoute()
    const store = useStore()
    const goods = ref({})
    const count = ref(1)
    // console.log('route', route)
    // 来个计算属性,计算一下库存量
    const countMax = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })
    async function getGoods () {
      const { result } = await reqFindGoods(route.params.id)
      // console.log('res', result)
      goods.value = result
    }
    getGoods()
    const currSku = ref({})
    const handleChange = (sku) => {
      // console.log('++++++sku出来了', sku)
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }
    // 加入购物车
    const insertCart = async () => {
      // console.log(goods.value)
      // 健全一下
      if (!currSku.value.skuId) {
        message({
          type: 'error',
          text: '请选择完整规格'
        })
        return
      }
      if (count.value > currSku.value.inventory) {
        message({
          type: 'error',
          text: '当前库存不足'
        })
        return
      }
      await store.dispatch('cart/asyncInsertCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: count.value // 商品数量
      })
      message({
        type: 'success',
        text: '加入购物车成功'
      })
    }
    return {
      goods,
      count,
      countMax,
      handleChange,
      insertCart,
      currSku
    }
  }

}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
